#include <iostream>
#include <UnidirectionLinkedList.hpp>

using namespace std;

class Solution
{
public:
    ListNode *rotateRight(ListNode *head, int k)
    {
        if (head == nullptr)
            return nullptr;
        ListNode *cur = head;
        int len = 1;
        while (cur->next != nullptr)
        {
            cur = cur->next;
            len++;
        }
        int move = k % len;
        cur->next = head;
        for (int i = 0; i < len - move; i++)
        {
            cur = cur->next;
        }
        ListNode *newHead = cur->next;
        cur->next = nullptr;
        return newHead;
    }
};

int main()
{
    cout << "Code61" << endl;
    Solution s;
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    ListNode *head = CreateLinkedList(arr, n);
    PrintLinkedList(head);
    ListNode *newHead = s.rotateRight(head, 2);
    PrintLinkedList(newHead);
    return 0;
}